<template>
  <div class="system-page-container layout-padding">
    <div class="system-page-padding layout-padding-auto layout-padding-view">
      <div class="system-user-search mb24">
        <el-input v-model="state.tableData.param.search" placeholder="请输入数据名称" style="max-width: 180px">
        </el-input>
        <el-button type="primary" class="ml10">
          <el-icon>
            <ele-Search v-model="state.tableData.param.name" />
          </el-icon>
          查询
        </el-button>
        <el-button type="success" class="ml10" @click="onOpenAddRole('add')">
          <el-icon>
            <ele-FolderAdd />
          </el-icon>
          新增
        </el-button>
      </div>
      <el-table
        :data="state.tableData.data"
        v-loading="state.tableData.loading"
        fit
        scrollbar-always-on
        show-overflow-tooltip
      >
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column prop="name" label="数据名称" min-width="160"></el-table-column>
        <el-table-column prop="digest" label="数据摘要" min-width="140"></el-table-column>
        <el-table-column prop="keyword" label="关键词" min-width="120"></el-table-column>
        <el-table-column prop="theme" label="数据主题" min-width="120"></el-table-column>
        <el-table-column prop="time" label="时间分辨率" min-width="140"></el-table-column>
        <el-table-column prop="space" label="空间分辨率" min-width="140"></el-table-column>
        <el-table-column prop="language" label="数据描述语言" min-width="160"></el-table-column>
        <el-table-column prop="discipline" label="学科分类" min-width="140"></el-table-column>
        <el-table-column prop="format" label="数据格式" min-width="140"></el-table-column>
        <el-table-column prop="type" label="数据类型" min-width="140"></el-table-column>
        <el-table-column prop="tbName" label="表名称" min-width="120"></el-table-column>
        <el-table-column label="操作" fixed="right" width="140">
          <template #default="scope">
            <el-button text type="primary" @click="onOpenEditRole('edit', scope.row)"> 修改 </el-button>
            <el-button text type="primary" @click="onRowDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex" style="justify-content: end">
        <el-pagination
          @size-change="onHandleSizeChange"
          @current-change="onHandleCurrentChange"
          class="mt15"
          :pager-count="5"
          :page-sizes="[10, 20, 30]"
          v-model:current-page="state.tableData.param.pageNum"
          background
          v-model:page-size="state.tableData.param.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="state.tableData.total"
        >
        </el-pagination>
      </div>
    </div>
    <editDialog ref="editDialogRef" @success="getTableData"></editDialog>
  </div>
</template>

<script setup lang="ts" name="tablePage">
import { ref, reactive, onMounted } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import editDialog from './modal/edit-dialog.vue';
import { getApiPage, deleteApi } from '@/api/ysj/index';

const editDialogRef = ref();

// 定义变量内容
const state = reactive<any>({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      name: '',
      pageNum: 1,
      pageSize: 10,
    },
  },
});
// 初始化表格数据
const getTableData = async () => {
  state.tableData.loading = true;
  const { list, total } = await getApiPage(state.param);
  state.tableData.data = list;
  state.tableData.total = total;
  state.tableData.loading = false;
};
// 打开新增弹窗
const onOpenAddRole = (type: string) => {
  editDialogRef.value.openDialog(type);
};
// 打开修改弹窗
const onOpenEditRole = (type: string, row: Object) => {
  editDialogRef.value.openDialog(type, row);
};

// 删除
const onRowDel = (row: any) => {
  ElMessageBox.confirm(`此操作将永久删除名称：“${row.name}”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(async () => {
      await deleteApi(row.id);
      getTableData();
      ElMessage.success('删除成功');
    })
    .catch(() => {});
};

// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};

// 页面加载时
onMounted(() => {
  getTableData();
});
</script>

<style scoped lang="scss">
.system-page-container {
  .system-page-padding {
    padding: 15px;
    .el-table {
      flex: 1;
    }
  }
}
</style>
